package com.ifdom.thread.threadlocal;

/**
 * 作用：提供线程隔离的局部变量
 * 多运行几次会发现线程中存储的数据错乱了。
 * @Author ifredomvip@gmail.com
 * @Date 2022/9/29 16:08
 * @Version 1.0.0
 * @Description
 **/

public class WithoutThreadLocalDemo {

    public String getContent() {
        return content;
    }

    private String content;

    private void setContent(String content) {
        this.content = content;
    }


    public static void main(String[] args) {

        WithoutThreadLocalDemo demo = new WithoutThreadLocalDemo();


        for (int i = 0; i < 10; i++) {

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    demo.setContent(Thread.currentThread().getName()+"数据");
                    System.out.println("执行任务: " + Thread.currentThread().getName() + "====数据：" + demo.getContent());
                }
            });

            thread.setName("线程" + i);
            thread.start();
        }

    }
}
